package com.cn.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.common.result.Result;
import com.cn.model.system.SysPost;
import com.cn.model.vo.SysPostQueryVo;
import com.cn.model.vo.SysUserQueryVo;
import com.cn.system.annotation.Log;
import com.cn.system.enums.BusinessType;
import com.cn.system.service.SysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "岗位管理")
@RestController
@RequestMapping("admin/system/sysPost")
@CrossOrigin
public class SysPostController {
    @Autowired
    private SysPostService sysPostService;

    @ApiOperation(value = "获取分页列表")
    @GetMapping("/{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam(name = "userQueryVo", value = "查询对象", required = false) SysPostQueryVo postQueryVo) {
        Page<SysPost> pageParam = new Page<>(page, limit);
        IPage<SysPost> pageModel = sysPostService.selectPage(pageParam, postQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取岗位")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysPost user = sysPostService.getById(id);
        return Result.ok(user);
    }

    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增岗位")
    @PostMapping("/save")
    public Result save(@RequestBody SysPost post) {
        boolean is_success = sysPostService.save(post);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更新岗位")
    @PutMapping("/update")
    public Result updateById(@RequestBody SysPost user) {
        sysPostService.updateById(user);
        return Result.ok();
    }

    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除岗位")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        sysPostService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        sysPostService.updateStatus(id,status);
        return Result.ok();
    }

}
